<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 化学品管理
 * @package app\chemical
 * @author LJQ<245369209@qq.com>
 */
class Chemical extends Api

{

    /**
   *  化学品列表
   * @param string $chemicalId 化学品ID
   * @return Json
   */

	public function chemicalList($scope = null, $chemicalId = null){
        $where= [] ;
        if ($scope) {
             $where['enterpriseId']=['in',$scope]; 
        }
        if($chemicalId){
        	$where['id'] = $chemicalId;
        }
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 化学品列表
			$info = collection(model('chemical')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//化学品列表
			$info = collection(model('chemical')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('chemical')->where($where)->count('id'); 
		 
	   	 return json(['code'=>1,'msg'=>'化学品','count'=>$count,'data'=>$info]);
       	
   	 	 }
   	  
   	      //添加化学品
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('basic/chemical');
          $result = $model->change($data);

          if (false != $result) {
            setlog('chemicaladd','chemical',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'化学品添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'化学品添加失败,请检查']);
          }
      }

   	

   /**
   * 修改化学品
   * @param  string chemicalId 化学品id,
   * @return Json
   */
   	public function edit($data,$chemicalId){
        
          if (empty($chemicalId)) {
              return json(['code'=>0,'msg'=>'化学品不存在,请检查']);
          }
          $model = model('chemical');     
          if ($model->change($data)) {
            setlog('chemicalupdate','chemical',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'化学品修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'化学品信息修改失败,请检查']);
            }
    }
    /**
   *  删除化学品
   * @param string chemicalId 化学品id,
   * @param Json
   */
     
    public function del($chemicalId) {
        $chemicalId = input('chemicalId',0,'intval');
        $model = model('chemical');
      	if (empty ( $chemicalId )||empty(($model->where(array('id'=>$chemicalId))->find()))) {
           	return  json(['code'=>0,'msg'=>'化学品id不存在,请检查']);
        }
        if($model->destroy(['id'=>$chemicalId]) !== FALSE){
          setlog('chemicaldel','chemical',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'化学品删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'化学品删除失败,请检查']);
       }
    }

     
   /**
   *   查询化学品
   * @param keyword化学品名称
   * @return Json
   */
   	public function search($keyword=null,$type=null){
   		$where = [] ;

		if($keyword){
			
			$where['chineseName'] = ['like','%'.$keyword.'%'];
		}

            if ($type) {
                   $where['typeId'] = ['eq',$type];
            }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//化学品列表
			$info =collection(model('chemical')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//化学品列表
			$info =collection(model('chemical')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('chemical')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到化学品','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'化学品','count'=>$count,'data'=>$info]);
       	}
      }
}